﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;
using System.Web.Configuration;

namespace ForestryEnterprise
{
    public partial class AddTransporter : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            updateDataView();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string statement = "select t_name, t_price, a_zip, a_city, a_state, a_strname, a_strnumb from Transporter where t_name=@tName and t_price=@tPrice and a_zip=@aZip and a_city=@aCity and a_state=@aState and a_strname=@aStrname and a_strnumb=@aStrnumb";
            OleDbConnection connection = new OleDbConnection(WebConfigurationManager.ConnectionStrings["forestryDB"].ConnectionString);
            OleDbCommand command = new OleDbCommand(statement, connection);
            command.Parameters.AddWithValue("@tName", this.TranName.Text);
            command.Parameters.AddWithValue("@tPrice", this.TranPrice.Text);
            command.Parameters.AddWithValue("@aZip", this.Zip.Text);
            command.Parameters.AddWithValue("@aCity", this.City.Text);
            command.Parameters.AddWithValue("@aState", this.State.Text);
            command.Parameters.AddWithValue("@aStrname", this.Strname.Text);
            command.Parameters.AddWithValue("@aStrnumb", this.Strnumber.Text);
            connection.Open();

            if (command.ExecuteReader().HasRows)
            {
                this.err.Text = "Transporter exists";
                connection.Close();
                return;
            }
            connection.Close();

            statement = "insert into Transporter(t_name, t_price, a_zip, a_city, a_state, a_strname, a_strnumb) values (@tName, @tPrice, @aZip, @aCity, @aState, @aStrname, @aStrnumb)";
            command = new OleDbCommand(statement, connection);
            connection.Open();
            command.Parameters.AddWithValue("@tName", this.TranName.Text);
            command.Parameters.AddWithValue("@tPrice", this.TranPrice.Text);
            command.Parameters.AddWithValue("@aZip", this.Zip.Text);
            command.Parameters.AddWithValue("@aCity", this.City.Text);
            command.Parameters.AddWithValue("@aState", this.State.Text);
            command.Parameters.AddWithValue("@aStrname", this.Strname.Text);
            command.Parameters.AddWithValue("@aStrnumb", this.Strnumber.Text);
            
            try
            {
                command.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
              err.Text = ex.Message;
            }

            connection.Close();
        }
        private void updateDataView()
        {
            string statement = "select * from transporter";
            this.TransData.SelectCommand = statement;
            this.TransDataView.DataBind();
        }

        protected void TransDataView_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.updateName.Text = this.TransDataView.SelectedRow.Cells[2].Text;
            this.updatePrice.Text = this.TransDataView.SelectedRow.Cells[3].Text;
            this.updateZip.Text = this.TransDataView.SelectedRow.Cells[4].Text;
            this.updateCity.Text = this.TransDataView.SelectedRow.Cells[5].Text;
            this.updateState.Text = this.TransDataView.SelectedRow.Cells[6].Text;
            this.updateStreetname.Text = this.TransDataView.SelectedRow.Cells[7].Text;
            this.updateStreetnumber.Text = this.TransDataView.SelectedRow.Cells[8].Text;
            this.updateItem.Visible = true;
        }

        protected void update_Click(object sender, EventArgs e)
        {
            string statement = "update Transporter set t_name=@tName, t_price=@tPrice, a_zip=@aZip, a_city=@aCity, a_state=@aState, a_strname=@aStrname, a_strnumb=@aStrnumb where ";
            statement += "T_id = " + this.TransDataView.SelectedRow.Cells[1].Text;
            OleDbConnection connection = new OleDbConnection(WebConfigurationManager.ConnectionStrings["forestryDB"].ConnectionString);
            OleDbCommand command = new OleDbCommand(statement, connection);
            command.Parameters.AddWithValue("@tName", this.updateName.Text);
            command.Parameters.AddWithValue("@tPrice", this.updatePrice.Text);
            command.Parameters.AddWithValue("@aZip", this.updateZip.Text);
            command.Parameters.AddWithValue("@aCity", this.updateCity.Text);
            command.Parameters.AddWithValue("@aState", this.updateState.Text);
            command.Parameters.AddWithValue("@aStrname", this.updateStreetname.Text);
            command.Parameters.AddWithValue("@aStrnumb", this.updateStreetnumber.Text);
            connection.Open();
            command.ExecuteNonQuery();
            connection.Close();
            this.updateItem.Visible = false;
            updateDataView();
        }

        protected void cancel_Click(object sender, EventArgs e)
        {
            this.updateItem.Visible = false;
        }

        
        
    }
}